package com.moss.cloud.auth.api.service.impl;

import com.moss.cloud.auth.api.config.ReadResourceConfig;
import com.moss.cloud.auth.api.service.IGeneralSecurityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Stream;

/**
 * 业务鉴权接口实现
 * @author 瑾年
 * @date 2023年3月7日
 */
@Service
@Slf4j
public class GeneralSecurityService implements IGeneralSecurityService {

    private final ReadResourceConfig readResourceConfig;

    public GeneralSecurityService(ReadResourceConfig readResourceConfig) {
        this.readResourceConfig = readResourceConfig;
    }

    @Override
    public boolean ignoreAuthentication(String url) {
        List<String> ignoreUrl = readResourceConfig.getIgnoreUrls();
        String [] urls = new String[ignoreUrl.size()];
        return Stream.of(ignoreUrl.toArray(urls))
                .anyMatch(item -> url.startsWith(StringUtils.trim(item)));
    }
}
